﻿using FB.Data.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Text;

namespace FB.Providers.EmployeeInfo
{
    public class EMPDbContext : DbContextBase
    {
        public DbSet<Employee> Employees { get; set; }
        public DbSet<Company> Companies { get; set; }
        public DbSet<Department> Departments { get; set; }
        public DbSet<DepartmentGroup> DepartmentGroups { get; set; }
        public DbSet<Team> Teams { get; set; }
        public DbSet<Position> Positions { get; set; }
        public DbSet<ProfilePic> ProfilePics { get; set; }
        public DbSet<EmployeeType> EmployeeTypes { get; set; }
        public DbSet<Sequence> Sequences { get; set; }
        public DbSet<EmployeeInPosition> EmployeeInPositions { get; set; }

        public DbSet<Holiday> Holidays { get; set; }

        //public DbSet<PrimarySetting> PrimarySettings { get; set; }
        

        public EMPDbContext() { throw new Exception("Not Implemented"); }
        internal EMPDbContext(string ConnectionStringName): base(ConnectionStringName)
        {
            Database.SetInitializer<EMPDbContext>(null);
            this.Database.Initialize(false);
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
            modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

            //modelBuilder.Entity<Employee>()
            //    .HasOptional(m => m.PrimarySetting)
            //    .WithRequired(m => m.Employee);

            //modelBuilder.Entity<Employee>()
            //    .HasMany(m => m.PostDetails)
            //    .WithRequired(m => m.Employee);
                
                
            modelBuilder.Entity<Employee>()
                .HasMany<Employee>(m => m.Subordinates)
                .WithMany(m => m.Supervisors)
                .Map(map =>
                {
                    map.ToTable("emp_SupervisingEmployees");
                    map.MapLeftKey("SupervisorCode");
                    map.MapRightKey("EMPCode");
                }); 
            
            //modelBuilder.Entity<Employee>()
            //    .HasMany(m => m.Companies)
            //    .WithMany(m => m.Employees)
            //    .Map(map =>
            //    {
            //        map.ToTable("emp_EmployeeInCompanies");
            //        map.MapLeftKey("EMPCode");
            //        map.MapRightKey("COMCode");
            //    });

            //modelBuilder.Entity<Employee>()
            //    .HasMany(m => m.Positions)
            //    .WithMany(m => m.Employees)
            //    .Map(map =>
            //    {
            //        map.ToTable("emp_EmployeeInPositions");
            //        map.MapLeftKey("EMPCode");
            //        map.MapRightKey("POSCode");
            //    });

            //modelBuilder.Entity<Employee>()
            //    .HasMany(m => m.Departments)
            //    .WithMany(m => m.Employees)
            //    .Map(map =>
            //    {
            //        map.ToTable("emp_EmployeeInDepartments");
            //        map.MapLeftKey("EMPCode");
            //        map.MapRightKey("DEPTCode");
            //    });

            //modelBuilder.Entity<Employee>()
            //    .HasMany(m => m.Teams)
            //    .WithMany(m => m.Employees)
            //    .Map(map =>
            //    {
            //        map.ToTable("emp_EmployeeInTeams");
            //        map.MapLeftKey("EMPCode");
            //        map.MapRightKey("TEAMCode");
            //    });


            base.OnModelCreating(modelBuilder);
        }
    }
}
